﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TAPARTK
    {

        public TAPARTK()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAPARTK();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();

            dwData = TMV.TBMain.NewRow();
            dwData["APAKMANDANT"] = app.Mandant;
            /*dwData["APAKANRKRS"] = " AND APAKANRKRS LIKE'" + KRS + "%'";
            dwData["APAKALFDNR"] = " AND APAKALFDNR LIKE'" + KRS + "%'";
            dwData["APAKAFOLGE"] = " AND APAKAFOLGE LIKE'" + KRS + "%'";
            dwData["APAKAEVOR"] = " AND APAKAEVOR LIKE'" + POS + "%'";*/
            SResult = TMV.QAPL04(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
         

        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            if (!IsNew)
            {
                IsNew = true; 
                return;
            }
            PM.Clear();
            PM["NRKRS"] = EXAUF.Substring(0, 2);
            PM["LFDNR"]   = EXAUF.Substring(2, 6);
            PM["FOLGE"]   = EXAUF.Substring(8, 3);
            PM["KSTKRS"] = EXAUF.Substring(11, 2);
            PM["KST"] = EXAUF.Substring(13, 4);
            PM["AGANG"] = EXAUF.Substring(17, 15);
            PM["ART"] = " ".PadRight(20);
            PM["VAR"] = " ".PadRight(3);
            PM["FINISH"] = " ".PadRight(10);
            PM["MTYP"] = " ".PadRight(8);
            PM["EVARHNR"] = " ".PadRight(2);
            PM["EVARUNR"] = " ".PadRight(2);
            PM["EVOR"] = TMV.APF.GETEVOR(PM);
            PM["WAKZ"] = ARTST;
            TMV.NEWAPEK(PM);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false; 
            base.NewCommand(sender, e);
        }

     
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 32)
                return;
            dwData = TMV.TBMain.NewRow();
            dwData["APAKMANDANT"] = app.Mandant;
            dwData["APAKANRKRS"] = " AND APAKANRKRS='" + EXAUF.Substring(0, 2) + "'";
            dwData["APAKALFDNR"] = " AND APAKALFDNR='" + EXAUF.Substring(2, 6) + "'";
            dwData["APAKAFOLGE"] = " AND APAKAFOLGE='" + EXAUF.Substring(8, 3) + "'";
            KST = EXAUF.Substring(11, 6);
            AGANG = EXAUF.Substring(17, 15);
            SResult = TMV.QAPL04(dwData);
            MergeData(SResult, true);
             
            base.OnEXAUFChg(vlaue);
        }
        public override void OnARTChg(string des)
        {
            if (ART.Length < 15 + 8 + 8 + 4)
                return;
            TMV.APAKMTYP = ART.Substring(15, 8);
            TMV.APAKEVARHNR = ART.Substring(31, 2);
            TMV.APAKEVARUNR = ART.Substring(33, 2);
            KEY = "";
            KEY = TMV.APAKANRKRS + TMV.APAKALFDNR + TMV.APAKAFOLGE + TMV.APAKAEVOR + TMV.APAKPKSTKRS + TMV.APAKPKST + TMV.APAKMTYP + TMV.APAKAGANG + TMV.APAKEVARHNR + TMV.APAKEVARUNR; 
            base.OnARTChg(des);
        }
        public override void OnAMTYPChg(string value)
        {
            if (AMTYP.Length == 0)
                return;
            TMV.APAKMTYP = AMTYP;
            base.OnAMTYPChg(value);
        }

        public override void OnVU1Chg(string value)
        {  
            if (dwMaster == null)
                return;
            if (VU1 == "")
                return;
            TMV.APAKBASMGE =double.Parse(VU1);
            TMV.UpdateAPARTK(dwMaster, 0);
            base.OnVU1Chg(value);
        } 
    }
}
